package com.java46.carera.reservationrecord.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.JsonResult;
import com.java46.carera.base.entity.ReservationRecord;
import com.java46.carera.base.exception.BusinessException;
import com.java46.carera.base.mapper.ReservationRecordMapper;
import com.java46.carera.base.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ReservationRecordService
 * @Description 预约Service
 * @Author c
 * @Date 2018/8/22 18:56
 * @Vcrsion 1.0
 **/
@Service
public class ReservationRecordService {
    @Autowired
    private ReservationRecordMapper reservationRecordMapper;

    /**
     * 添加预约记录
     * @param reservationRecord
     * @return
     */
    public JsonResult addReservationRecord(ReservationRecord reservationRecord){
        if(reservationRecord == null){
            throw new BusinessException("添加的信息不能为空！");
        }
        reservationRecordMapper.insert(reservationRecord);
        return JsonUtil.setJsonResult(true,"添加成功！",null);
    }

    /**
     * 修改预约记录
     * @param reservationRecord
     * @return
     */
    public JsonResult modReservationRecord(ReservationRecord reservationRecord){
        if(reservationRecord.getResid() == null){
            throw new BusinessException("缺少修改的必要参数！");
        }
        reservationRecordMapper.updateByPrimaryKey(reservationRecord);
        return JsonUtil.setJsonResult(true,"修改成功！",null);
    }

    /**
     * 修改预约处理状态
     * @param resid
     * @return
     */
    public JsonResult updateByStatus(Integer resid){
        if(resid == null){
            throw new BusinessException("缺少修改的必要参数！");
        }
        reservationRecordMapper.updateByStatus(resid);
        return JsonUtil.setJsonResult(true,"修改成功！",null);
    }

    /**
     * 删除预约记录
     * @param resid
     * @return
     */
    public JsonResult deleteReservationRecord(Integer resid){
        if(resid == null){
            throw new BusinessException("缺少删除的必要参数！");
        }
        reservationRecordMapper.deleteByPrimaryKey(resid);
        return JsonUtil.setJsonResult(true,"删除成功！",null);
    }

    /**
     * 模糊查询所有预约记录
     * @param keyword
     * @param pageNum
     * @return
     */
    public JsonResult queryAll(String keyword, Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<ReservationRecord> reservationRecords = reservationRecordMapper.selectAll(keyword);
        PageInfo<ReservationRecord> pages = new PageInfo<>(reservationRecords);
        return JsonUtil.setJsonResult(true,"查询成功！",pages);
    }

    /**
     * 根据编号查询预约记录
     * @param resid
     * @return
     */
    public JsonResult queryById(Integer resid){
        if(resid == null){
            throw new BusinessException("缺少查询的必要参数！");
        }
        ReservationRecord reservationRecord = reservationRecordMapper.selectByPrimaryKey(resid);
        return JsonUtil.setJsonResult(true,"查询成功！",reservationRecord);
    }
}
